import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    //主页
    path: "/home",
    component: () => import("../views/Home.vue"),
    children: [
      {
        //首页
        path: "/home",
        redirect: "/home/index",
      },
      {
        path: "/home/index",
        component: () => import("../views/Home/index.vue"),
      },
      {
        //寻医
        path: "/home/finddoctor",
        component: () => import("../views/Home/finddoctor.vue"),
        // 三级路由
        children: [
          {
            path: "/home/finddoctor",
            redirect: "/home/finddoctor/tuijian",
          },
          {
            path: "/home/finddoctor/erke",
            component: () => import("../views/findd/Erke.vue"),
          },
          {
            path: "/home/finddoctor/fuchanke",
            component: () => import("../views/findd/Fuchanke.vue"),
          },
          {
            path: "/home/finddoctor/neke",
            component: () => import("../views/findd/Neke.vue"),
          },
          {
            path: "/home/finddoctor/pifuke",
            component: () => import("../views/findd/Pifuke.vue"),
          },
          {
            path: "/home/finddoctor/tuijian",
            component: () => import("../views/findd/Tuijian.vue"),
          },
          {
            path: "/home/finddoctor/waike",
            component: () => import("../views/findd/Waike.vue"),
          },
        ],
      },
      {
        //健康头条
        path: "/home/healthheader",
        component: () => import("../views/Home/healthheader.vue"),
        children: [
          {
            path: "/home/healthheader",
            redirect: "/home/healthheader/doctor",
          },
          {
            //医生说
            path: "/home/healthheader/doctor",
            component: () => import("../views/Home/healthheader/doctor.vue"),
          },
          {
            //推荐
            path: "/home/healthheader/recommend",
            component: () => import("../views/Home/healthheader/recommend.vue"),
          },
          {
            //直播
            path: "/home/healthheader/live",
            component: () => import("../views/Home/healthheader/live.vue"),
          },
          {
            //名医课
            path: "/home/healthheader/curriculum",
            component: () =>
              import("../views/Home/healthheader/curriculum.vue"),
          },
        ],
      },
      {
        //健康商城
        path: "/home/healthshop",
        component: () => import("../views/Home/healthshop.vue"),
      },
      {
        //我的
        path: "/home/mine",
        component: () => import("../views/Home/mine.vue"),
      },
    ],
  },
  {
    //详情
    path: "/detail/:id",
    component: () => import("../views/Detail.vue"),
  },
  {
    //vip
    path: "/vip",
    component: () => import("../views/VIP.vue"),
  },
  {
    //专科频道
    path: "/channel",
    component: () => import("../views/Channel.vue"),
  },
  {
    //地址
    path: "/aduss",
    component: () => import("../views/Aduss.vue"),
  },
  {
    //登录
    path: "/login",
    component: () => import("../views/Login.vue"),
  },
  {
    //预约挂号
    path: "/list",
    component: () => import("../views/list.vue"),
  },
  {
    //名医工作室
    path: "/studio",
    component: () => import("../views/studio.vue"),
    children: [
      {
        path: "/studio",
        redirect: "/studio/fuchan",
      },
      {
        path: "/studio/chaoshen",
        component: () => import("../views/studio/chaoshen.vue"),
      },
      {
        path: "/studio/erke",
        component: () => import("../views/studio/erke.vue"),
      },
      {
        path: "/studio/erpihou",
        component: () => import("../views/studio/erpihou.vue"),
      },
      {
        path: "/studio/fuchan",
        component: () => import("../views/studio/fuchan.vue"),
      },
      {
        path: "/studio/gengduo",
        component: () => import("../views/studio/gengduo.vue"),
      },
      {
        path: "/studio/kouqing",
        component: () => import("../views/studio/kouqing.vue"),
      },
      {
        path: "/studio/neike",
        component: () => import("../views/studio/neike.vue"),
      },
      {
        path: "/studio/pifike",
        component: () => import("../views/studio/pifike.vue"),
      },
      {
        path: "/studio/shengchi",
        component: () => import("../views/studio/shengchi.vue"),
      },
      {
        path: "/studio/waike",
        component: () => import("../views/studio/waike.vue"),
      },
      {
        path: "/studio/xinli",
        component: () => import("../views/studio/xinli.vue"),
      },
      {
        path: "/studio/yanke",
        component: () => import("../views/studio/yanke.vue"),
      },
      {
        path: "/studio/zhongliu",
        component: () => import("../views/studio/zhongliu.vue"),
      },
      {
        path: "/studio/zhongyi",
        component: () => import("../views/studio/zhongyi.vue"),
      },
    ],
  },
  {
    //急速咨询
    path: "/quick",
    component: () => import("../views/Quickconsultation.vue"),
    meta: {
      visitflag: true,
    },
  },
  {
    //在家购药
    path: "/athome",
    component: () => import("../views/BuymedatHome.vue"),
  },
  {
    //钱包
    path: "/money",
    component: () => import("../views/Money.vue"),
  },
  {
    //在线咨询
    path: "/online",
    component: () => import("../views/Online.vue"),
  },
  {
    //专家义诊
    path: "/zhuanjia",
    component: () => import("../views/zhuanjia.vue"),
    children: [
      {
        path: "/zhuanjia",
        redirect: "/zhuanjia/yizheng",
      },
      {
        path: "/zhuanjia/yizheng",
        component: () => import("../views/yizheng.vue"),
      },
    ],
  },
  {
    //404
    path: "/:pathMatch(.*)*",
    component: () => import("../views/404.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  document.documentElement.scrollTop = 0;
  if (to.meta.visitflag === true) {
    sessionStorage.setItem("router", "#/quick");
    if (sessionStorage.getItem("sign")) {
      next();
    } else {
      if (sessionStorage.getItem("router")) {
        next("/login");
      } else {
        next("/home");
      }
    }
  } else {
    next();
  }
});
export default router;
